package com.nuoniu.sibanyun.controller.erp.in;


import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.erp.in.InInventory;
import com.nuoniu.sibanyun.entity.erp.in.InUnitGroup;
import com.nuoniu.sibanyun.service.erp.IErpAccountService;
import com.nuoniu.sibanyun.service.erp.in.IInInventoryService;
import com.nuoniu.sibanyun.service.erp.in.IInUnitGroupService;
import com.nuoniu.sibanyun.service.erp.in.IInUnitService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 计量单位分组 前端控制器
 * </p>
 *
 * @author baiqiang
 * @since 2020-09-25
 */
@RestController
@RequestMapping("/in/unitGroup")
@Api(value = "计量单位分组", tags = "计量单位分组")
public class InUnitGroupController extends BaseController {

    @Autowired
    private IInUnitGroupService unitGroupService;
    @Autowired
    private IInUnitService unitService;
    @Autowired
    private IErpAccountService accountService;
    @Autowired
    private IInInventoryService inventoryService;

    /**
     * 获取分组列表
     * @author 小啊刚
     * @date 2020-09-25
     */
    @GetMapping("list")
    @ApiOperation(value = "获取计量分组列表")
    public HttpResult getUnitGroupList() {
        return HttpResult.ok(unitGroupService.getUnitGroupList());
    }

    /**
     * 保存计量分组
     * @author 小啊刚
     * @date 2020-09-25
     */
    @PostMapping("save")
    @ApiOperation(value = "保存计量分组")
    public HttpResult save(@RequestBody InUnitGroup unitGroup) {
        if(StringUtils.isBlank(unitGroup.getName())) {
            throw new NuoNiuException("计量分组名称不能为空");
        }
        if(StringUtils.isBlank(unitGroup.getCode())) {
            throw new NuoNiuException("计量分组编码不能为空");
        }

        UserInfo userInfo = UserThreadLocal.get();
        unitGroup.setCompanyId(userInfo.getCompanyId());
        if(unitGroup.getId() != null) {
            unitGroup.setUpdateUserId(userInfo.getUserId());
            unitGroup.setUpdateDate(LocalDateTime.now());
        }else {
            unitGroup.setAccountId(accountService.getCurrentAccountId());
            unitGroup.setCreateUserId(userInfo.getUserId());
        }
        unitGroupService.saveOrUpdate(unitGroup);
        return HttpResult.ok();
    }

    /**
     * 删除计量分组
     * @author 小啊刚
     * @date 2020-09-25
     */
    @DeleteMapping("delete")
    @ApiOperation(value = "删除计量分组")
    public HttpResult delete(Integer groupId) {
        List<InInventory> inventoryList = inventoryService.getByUnitGroupId(groupId);
        if(inventoryList.size() > 0) {
            throw new NuoNiuException("已在物料中使用,不能删除");
        }
        unitService.getRemoveByGroupId(groupId);
        unitGroupService.removeById(groupId);
        return HttpResult.ok();
    }
}
